package com.example.gamedemo;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Random;

import com.example.gamedemo.util.SystemUiHider;

import android.R.layout;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.transition.Scene;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.AbsoluteLayout.LayoutParams;

public class Game1 extends Activity {

	TextView tvChayTGian, tvDiem, tvPhepTinh, tvPhepTinh2;
	Button btnDung, btnSai;
	AbsoluteLayout abs, abs2;
	LinearLayout layout1, layout2;

	int[] arrInt = new int[150];
	int[] arrColor = { -16737844, -13337556, -494543, -13336553, -10392194,
			-11650946, -14329730, -4040068, -12870500, -13599362, -13075388,
			-8226759, -8436150 };

	int intXTextView;
	int intYTextView;
	int intXPhepTinh;
	int intYPhepTinh;
	int intXPhepTinh2;
	int intYPhepTinh2;
	int screenWidth;
	int screenHeight;
	int numberSleep;
	int Sa, Sb, Sc, Sum, SumTemp;
	int khoitao, count, cds, score = 0, bestScore = 0;
	String memTemp;
	int intFile;
	int randomColor;
	int i;
	int dieukien; //dieu kien khong chay lai tg khi nhan choi lai
	boolean cds_Thread = true;

	boolean istrue = false;
	boolean istrue2 = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_game1);
		tvChayTGian = (TextView) findViewById(R.id.tvChayTGian_Game1_GameDemo);
		tvDiem = (TextView) findViewById(R.id.tvDiem_Game1_GameDemo);
		tvPhepTinh = (TextView) findViewById(R.id.tvPhepTinh_Game1_GameDemo);
		tvPhepTinh2 = (TextView) findViewById(R.id.tvPhepTinh2_Game1_GameDemo);
		btnDung = (Button) findViewById(R.id.btnDung_Game1_GameDemo);
		btnSai = (Button) findViewById(R.id.btnSai_Game1_GameDemo);

		abs = (AbsoluteLayout) findViewById(R.id.absLaytout_Game1_GameDemo);
		abs2 = (AbsoluteLayout) findViewById(R.id.absLayout2_Game1_GameDemo);
		layout1 = (LinearLayout) findViewById(R.id.layout1_Game1_GameDemo);
		layout2 = (LinearLayout) findViewById(R.id.layout2_Game1_GameDemo);

		absParams = (LayoutParams) tvChayTGian.getLayoutParams();
		intXTextView = absParams.x;
		intYTextView = absParams.y;

		absParamsPhepTinh = (LayoutParams) tvPhepTinh.getLayoutParams();
		intXPhepTinh = absParamsPhepTinh.x;
		intYPhepTinh = absParamsPhepTinh.y;
		
		absParamsPhepTinh2 = (LayoutParams) tvPhepTinh2.getLayoutParams();
		intXPhepTinh2 = absParamsPhepTinh2.x;
		intYPhepTinh2 = absParamsPhepTinh2.y;

		// ----------Program--------------
		NumberSleep();
		KhoiTaoPhepTinhDauTien();
		ChonDung();
		ChonSai();

		// -------------------------------

	}

	private void ReadData() {
		// TODO Auto-generated method stub
		try {
			FileInputStream in = openFileInput("memory_game1.txt");
			BufferedReader reader = new BufferedReader(
					new InputStreamReader(in));
			String data = "";
			StringBuilder builder = new StringBuilder();
			while ((data = reader.readLine()) != null) {
				builder.append(data);
				builder.append("\n");
			}
			in.close();
			// editdata.setText(builder.toString());
			memTemp = builder.toString();
			memTemp.replaceAll("\n", " ");
			intFile = Integer.parseInt(memTemp.trim());

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void WriteData() {
		// TODO Auto-generated method stub
		try {
			FileOutputStream out = openFileOutput("memory_game1.txt", 0);
			OutputStreamWriter writer = new OutputStreamWriter(out);
			writer.write(bestScore + "");
			writer.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	AbsoluteLayout.LayoutParams absParams;

	Handler myHandlerMove = new Handler() {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if ((intXTextView > (-screenWidth - 10))
					&& (intXTextView <= -screenWidth)) {
				Intent intent = new Intent(Game1.this, ServiceMusicSai.class);
				startService(intent);
				if (cds_Thread) {
					istrue = false;
					intXTextView = 0;
					currentThread = createThread();
					currentThread.start();
					cds_Thread = false;
					ReadData(); // CHo nay chieu xem lai
					bestScore = intFile;
					MyAlert();
				}

			}

			absParams.x = intXTextView;
			absParams.y = intYTextView;
			tvChayTGian.setLayoutParams(absParams);
		}
	};

	AbsoluteLayout.LayoutParams absParamsPhepTinh;
	AbsoluteLayout.LayoutParams absParamsPhepTinh2;

	Handler myHandlerPhepTinh = new Handler() {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			absParamsPhepTinh.x = intXPhepTinh;
			absParamsPhepTinh2.x = intXPhepTinh2;
			tvPhepTinh.setLayoutParams(absParamsPhepTinh);
			tvPhepTinh2.setLayoutParams(absParamsPhepTinh2);
		}
	};

	// -------------Chon Sai--------------------

	private void ChonSai() {
		// TODO Auto-generated method stub
		btnSai.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (dieukien == 0) {
					dieukien = 1;
					intXTextView = 0;
					istrue = true;
					currentThread = createThread();
					currentThread.start();
				}
					randomColor = (int) (Math.random() * arrColor.length);
					layout1.setBackgroundColor(arrColor[randomColor]);
					layout2.setBackgroundColor(arrColor[randomColor]);
					++count;
					intXTextView = 0;
					if (arrInt[count - 1] == 0) {
					Intent intent = new Intent(Game1.this,
							ServiceMusicDung.class);
					startService(intent);
					++score;
					ReadData();
					bestScore = intFile;
					if (score > bestScore) {
						bestScore = score;
						WriteData();
					}
					tvDiem.setText(score + "");
					istrue2 = true;
					PhepTinhThread = createThreadPhepTinh();
					PhepTinhThread.start();
					PhepTinh();
					if (cds == 1) {
						arrInt[count] = 1;
					} else if (cds == 0) {
						arrInt[count] = 0;
					}

				} else if (arrInt[count - 1] == 1) {
					istrue = false;
					intXTextView = 0;
					currentThread = createThread();
					currentThread.start();
					ReadData();
					bestScore = intFile;
					Intent intent = new Intent(Game1.this,
							ServiceMusicSai.class);
					startService(intent);
					MyAlert();
				}
			}
		});
	}

	// ----------------Chon Dung-----------------

	private void ChonDung() {
		// TODO Auto-generated method stub
		btnDung.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (dieukien == 0) {
					dieukien = 1;
					intXTextView = 0;
					istrue = true;
					currentThread = createThread();
					currentThread.start();
				}
				randomColor = (int) (Math.random() * arrColor.length);
				layout1.setBackgroundColor(arrColor[randomColor]);
				layout2.setBackgroundColor(arrColor[randomColor]);
				++count;
				intXTextView = 0;
				if (arrInt[count - 1] == 1) {
					Intent intent = new Intent(Game1.this,
							ServiceMusicDung.class);
					startService(intent);
					++score;
					ReadData();
					bestScore = intFile;
					if (score > bestScore) {
						bestScore = score;
						WriteData();
					}
					tvDiem.setText(score + "");
					istrue2 = true;
					PhepTinhThread = createThreadPhepTinh();
					PhepTinhThread.start();
					PhepTinh();
					if (cds == 1) {
						arrInt[count] = 1;
					} else if (cds == 0) {
						arrInt[count] = 0;
					}

				} else if (arrInt[count - 1] == 0) {
					istrue = false;
					intXTextView = 0;
					currentThread = createThread();
					currentThread.start();
					ReadData();
					bestScore = intFile;
					Intent intent = new Intent(Game1.this,
							ServiceMusicSai.class);
					startService(intent);
					MyAlert();

				}
			}
		});
	}

	// -----------------My Alert-----------------------
	protected void MyAlert() {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		AlertDialog.Builder ht = new AlertDialog.Builder(Game1.this);
		ht.setTitle("Sai mất rồi");
		ht.setMessage("Your Score: " + score + "\nBest Score: " + bestScore);
		ht.setNegativeButton("Chơi lại", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
				PhepTinh();

				cds_Thread = true;
				if (cds == 1) {
					arrInt[0] = 1;
				} else if (cds == 0) {
					arrInt[0] = 0;
				}
				intXTextView = 0;
				dieukien = 0;
//				istrue = true;
			}

		});
		ht.setPositiveButton("Thoát", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
				finish();
				Intent intent1 = new Intent(Game1.this, ServiceMusic.class);
				startService(intent1);
			}
		});

		// ------default các giá tr?-------------
		count = 0;
		score = 0;
		tvDiem.setText("0");
		// --------------------------------------
		ht.setCancelable(false);
		ht.create().show();
	}

	// -------------Khoi Tao Phep Tinh Ban Dau ------------------

	private void KhoiTaoPhepTinhDauTien() {
		// TODO Auto-generated method stub
		if (khoitao == 0) {
			khoitao = 1;
			randomColor = (int) (Math.random() * arrColor.length);
			layout1.setBackgroundColor(arrColor[randomColor]);
			layout2.setBackgroundColor(arrColor[randomColor]);
			PhepTinh();
			if (cds == 1) {
				arrInt[0] = 1;
			} else if (cds == 0) {
				arrInt[0] = 0;
			}

		}
	}

	// ------------Phep Tinh-------------------
	protected void PhepTinh() {
		// TODO Auto-generated method stub
		if (score <= 4) {
			Sa = (int) (Math.random() * 5);
			Sb = (int) (Math.random() * 5);
			SumTemp = Sa + Sb;
			Sum = SumTemp + ((int) (Math.random() * (3)) + 1);
			cds = (int) (Math.random() * 2);
			if (cds == 1) {
				tvPhepTinh.setText(Sa + " + " + Sb + "\n" + "  = " + SumTemp);
				tvPhepTinh2.setText(Sa + " + " + Sb + "\n" + "  = " + SumTemp);
			} else if (cds == 0) {
				tvPhepTinh.setText(Sa + " + " + Sb + "\n" + "  = " + Sum);
				tvPhepTinh2.setText(Sa + " + " + Sb + "\n" + "  = " + Sum);
			}

		}

		else if (5 <= score && score <= 8) {
			Sa = (int) (Math.random() * 10);
			Sb = (int) (Math.random() * 10);
			SumTemp = Sa + Sb;
			Sum = SumTemp + ((int) (Math.random() * (3)) + 1);
			cds = (int) (Math.random() * 2);
			if (cds == 1) {
				tvPhepTinh.setText(Sa + " + " + Sb + "\n" + "  = " + SumTemp);
				tvPhepTinh2.setText(Sa + " + " + Sb + "\n" + "  = " + SumTemp);
			} else if (cds == 0) {
				tvPhepTinh.setText(Sa + " + " + Sb + "\n" + "  = " + Sum);
				tvPhepTinh2.setText(Sa + " + " + Sb + "\n" + "  = " + Sum);
			}
		}

		else if (9 <= score && score <= 13) {
			int i = (int) (Math.random() * 2);
			if (i == 0) {
				Sa = (int) (Math.random() * 10);
				Sb = (int) (Math.random() * 10);
				SumTemp = Sa + Sb;
				Sum = SumTemp + ((int) (Math.random() * (3)) + 1);
				cds = (int) (Math.random() * 2);
				if (cds == 1) {
					tvPhepTinh.setText(Sa + " + " + Sb + "\n" + "  = "
							+ SumTemp);
					tvPhepTinh2.setText(Sa + " + " + Sb + "\n" + "  = "
							+ SumTemp);
				} else if (cds == 0) {
					tvPhepTinh.setText(Sa + " + " + Sb + "\n" + "  = " + Sum);
					tvPhepTinh2.setText(Sa + " + " + Sb + "\n" + "  = " + Sum);
				}
			} else if (i == 1) {
				Sa = (int) (Math.random() * 10);
				Sb = (int) (Math.random() * 10);
				SumTemp = Sa - Sb;
				Sum = SumTemp + ((int) (Math.random() * (3)) + 1);
				cds = (int) (Math.random() * 2);
				if (cds == 1) {
					tvPhepTinh.setText(Sa + " - " + Sb + "\n" + "  = "
							+ SumTemp);
					tvPhepTinh2.setText(Sa + " - " + Sb + "\n" + "  = "
							+ SumTemp);
				} else if (cds == 0) {
					tvPhepTinh.setText(Sa + " + " + Sb + "\n" + "  = " + Sum);
					tvPhepTinh2.setText(Sa + " + " + Sb + "\n" + "  = " + Sum);
				}
			}

		}

		else if (14 <= score && score <= 19) {
			int i = (int) (Math.random() * 2);
			if (i == 0) {
				Sa = (int) (Math.random() * 20);
				Sb = (int) (Math.random() * 20);
				SumTemp = Sa + Sb;
				Sum = SumTemp + ((int) (Math.random() * (3)) + 1);
				cds = (int) (Math.random() * 2);
				if (cds == 1) {
					tvPhepTinh.setText(Sa + " + " + Sb + "\n" + "  = "
							+ SumTemp);
					tvPhepTinh2.setText(Sa + " + " + Sb + "\n" + "  = "
							+ SumTemp);
				} else if (cds == 0) {
					tvPhepTinh.setText(Sa + " + " + Sb + "\n" + "  = " + Sum);
					tvPhepTinh2.setText(Sa + " + " + Sb + "\n" + "  = " + Sum);
				}
			} else if (i == 1) {
				Sa = (int) (Math.random() * 20);
				Sb = (int) (Math.random() * 20);
				SumTemp = Sa - Sb;
				Sum = SumTemp + ((int) (Math.random() * (3)) + 1);
				cds = (int) (Math.random() * 2);
				if (cds == 1) {
					tvPhepTinh.setText(Sa + " - " + Sb + "\n" + "  = "
							+ SumTemp);
					tvPhepTinh2.setText(Sa + " - " + Sb + "\n" + "  = "
							+ SumTemp);
				} else if (cds == 0) {
					tvPhepTinh.setText(Sa + " - " + Sb + "\n" + "  = " + Sum);
					tvPhepTinh2.setText(Sa + " - " + Sb + "\n" + "  = " + Sum);
				}
			}

		}

		else if (20 <= score && score <= 24) {
			int i = (int) (Math.random() * 2);
			int j = (int) (Math.random() * 2);
			if (i == 0) {
				if (j == 0) {
					Sa = (int) (Math.random() * 10);
					Sb = (int) (Math.random() * 10);
					Sc = (int) (Math.random() * 10);
					SumTemp = Sa + Sb + Sc;
					Sum = SumTemp + ((int) (Math.random() * (3)) + 1);
					cds = (int) (Math.random() * 2);
					if (cds == 1) {
						tvPhepTinh.setText(Sa + " + " + Sb + " + " + Sc + "\n"
								+ "  = " + SumTemp);
						tvPhepTinh2.setText(Sa + " + " + Sb + " + " + Sc + "\n"
								+ "  = " + SumTemp);
					} else if (cds == 0) {
						tvPhepTinh.setText(Sa + " + " + Sb + " + " + Sc + "\n"
								+ "  = " + Sum);
						tvPhepTinh2.setText(Sa + " + " + Sb + " + " + Sc + "\n"
								+ "  = " + Sum);
					}

				} else if (j == 1) {
					Sa = (int) (Math.random() * 10);
					Sb = (int) (Math.random() * 10);
					Sc = (int) (Math.random() * 10);
					SumTemp = Sa + Sb - Sc;
					Sum = SumTemp + ((int) (Math.random() * (3)) + 1);
					cds = (int) (Math.random() * 2);
					if (cds == 1) {
						tvPhepTinh.setText(Sa + " + " + Sb + " - " + Sc + "\n"
								+ "  = " + SumTemp);
						tvPhepTinh2.setText(Sa + " + " + Sb + " - " + Sc + "\n"
								+ "  = " + SumTemp);
					} else if (cds == 0) {
						tvPhepTinh.setText(Sa + " + " + Sb + " - " + Sc + "\n"
								+ "  = " + Sum);
						tvPhepTinh2.setText(Sa + " + " + Sb + " - " + Sc + "\n"
								+ "  = " + Sum);
					}
				}
			} else if (i == 1) {
				if (j == 0) {
					Sa = (int) (Math.random() * 10);
					Sb = (int) (Math.random() * 10);
					Sc = (int) (Math.random() * 10);
					SumTemp = Sa - Sb + Sc;
					Sum = SumTemp + ((int) (Math.random() * (3)) + 1);
					cds = (int) (Math.random() * 2);
					if (cds == 1) {
						tvPhepTinh.setText(Sa + " - " + Sb + " + " + Sc + "\n"
								+ "  = " + SumTemp);
						tvPhepTinh2.setText(Sa + " - " + Sb + " + " + Sc + "\n"
								+ "  = " + SumTemp);
					} else if (cds == 0) {
						tvPhepTinh.setText(Sa + " - " + Sb + " + " + Sc + "\n"
								+ "  = " + Sum);
						tvPhepTinh2.setText(Sa + " - " + Sb + " + " + Sc + "\n"
								+ "  = " + Sum);
					}
				} else if (j == 1) {
					Sa = (int) (Math.random() * 10);
					Sb = (int) (Math.random() * 10);
					Sc = (int) (Math.random() * 10);
					SumTemp = Sa - Sb - Sc;
					Sum = SumTemp + ((int) (Math.random() * (3)) + 1);
					cds = (int) (Math.random() * 2);
					if (cds == 1) {
						tvPhepTinh.setText(Sa + " - " + Sb + " - " + Sc + "\n"
								+ "  = " + SumTemp);
						tvPhepTinh2.setText(Sa + " - " + Sb + " - " + Sc + "\n"
								+ "  = " + SumTemp);
					} else if (cds == 0) {
						tvPhepTinh.setText(Sa + " - " + Sb + " - " + Sc + "\n"
								+ "  = " + Sum);
						tvPhepTinh2.setText(Sa + " - " + Sb + " - " + Sc + "\n"
								+ "  = " + Sum);
					}
				}
			}

		}

		else if (25 <= score) {
			int i = (int) (Math.random() * 2);
			int j = (int) (Math.random() * 2);
			if (i == 0) {
				if (j == 0) {
					Sa = (int) (Math.random() * 20);
					Sb = (int) (Math.random() * 20);
					Sc = (int) (Math.random() * 20);
					SumTemp = Sa + Sb + Sc;
					Sum = SumTemp + ((int) (Math.random() * (3)) + 1);
					cds = (int) (Math.random() * 2);
					if (cds == 1) {
						tvPhepTinh.setText(Sa + " + " + Sb + " + " + Sc + "\n"
								+ "  = " + SumTemp);
						tvPhepTinh2.setText(Sa + " + " + Sb + " + " + Sc + "\n"
								+ "  = " + SumTemp);
					} else if (cds == 0) {
						tvPhepTinh.setText(Sa + " + " + Sb + " + " + Sc + "\n"
								+ "  = " + Sum);
						tvPhepTinh2.setText(Sa + " + " + Sb + " + " + Sc + "\n"
								+ "  = " + Sum);
					} else if (j == 1) {
						Sa = (int) (Math.random() * 20);
						Sb = (int) (Math.random() * 20);
						Sc = (int) (Math.random() * 20);
						SumTemp = Sa + Sb - Sc;
						Sum = SumTemp + ((int) (Math.random() * (3)) + 1);
						cds = (int) (Math.random() * 2);
						if (cds == 1) {
							tvPhepTinh.setText(Sa + " + " + Sb + " - " + Sc
									+ "\n" + "  = " + SumTemp);
							tvPhepTinh2.setText(Sa + " + " + Sb + " - " + Sc
									+ "\n" + "  = " + SumTemp);
						} else if (cds == 0) {
							tvPhepTinh.setText(Sa + " + " + Sb + " - " + Sc
									+ "\n" + "  = " + Sum);
							tvPhepTinh2.setText(Sa + " + " + Sb + " - " + Sc
									+ "\n" + "  = " + Sum);
						}
					}
				} else if (i == 1) {
					if (j == 0) {
						Sa = (int) (Math.random() * 20);
						Sb = (int) (Math.random() * 20);
						Sc = (int) (Math.random() * 20);
						SumTemp = Sa - Sb + Sc;
						Sum = SumTemp + ((int) (Math.random() * (3)) + 1);
						cds = (int) (Math.random() * 2);
						if (cds == 1) {
							tvPhepTinh.setText(Sa + " - " + Sb + " + " + Sc
									+ "\n" + "  = " + SumTemp);
							tvPhepTinh2.setText(Sa + " - " + Sb + " + " + Sc
									+ "\n" + "  = " + SumTemp);
						} else if (cds == 0) {
							tvPhepTinh.setText(Sa + " - " + Sb + " + " + Sc
									+ "\n" + "  = " + Sum);
							tvPhepTinh2.setText(Sa + " - " + Sb + " + " + Sc
									+ "\n" + "  = " + Sum);
						} else if (j == 1) {
							Sa = (int) (Math.random() * 20);
							Sb = (int) (Math.random() * 20);
							Sc = (int) (Math.random() * 20);
							SumTemp = Sa - Sb - Sc;
							Sum = SumTemp + ((int) (Math.random() * (3)) + 1);
							cds = (int) (Math.random() * 2);
							if (cds == 1) {
								tvPhepTinh.setText(Sa + " - " + Sb + " - " + Sc
										+ "\n" + "  = " + SumTemp);
								tvPhepTinh2.setText(Sa + " - " + Sb + " - " + Sc
										+ "\n" + "  = " + SumTemp);
							} else if (cds == 0) {
								tvPhepTinh.setText(Sa + " - " + Sb + " - " + Sc
										+ "\n" + "  = " + Sum);
								tvPhepTinh2.setText(Sa + " - " + Sb + " - " + Sc
										+ "\n" + "  = " + Sum);
							}

						}
					}
				}
			}
		}
	}

	// -------------NumberSleep--------------------

	private void NumberSleep() {
		// TODO Auto-generated method stub
		Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
				.getDefaultDisplay();
		screenWidth = display.getWidth();
		screenHeight = display.getHeight();

		numberSleep = (int) (1000 / screenWidth);
	}

	// -------------setToaDo-----------------------
	Thread currentThread = null;

	public Thread createThread() {
		// TODO Auto-generated method stub
		Thread threadMoveTextView = new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				while (istrue) { // Muon dung lai minh them dieu kien trong
									// ngoac
					// cua while
					Message msg = myHandlerMove.obtainMessage();
					intXTextView--;
					if ((intXTextView > (-screenWidth - 10))
							&& (intXTextView <= -screenWidth)) {
						istrue = false;
					}
					myHandlerMove.sendMessage(msg);
					try {
						Thread.sleep((int) 2000 / screenWidth); // numberSleep
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		});
		return threadMoveTextView;
	}

	Thread PhepTinhThread = null;

	public Thread createThreadPhepTinh() {
		Thread threadMovePhepTinh = new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				while (istrue2) { // Muon dung lai minh them dieu kien trong
									// ngoac
					// cua while
					Message msg = myHandlerPhepTinh.obtainMessage();
					if(i == 0){
						i = 1;
						intXPhepTinh2 = screenWidth;
					}
					intXPhepTinh = intXPhepTinh - 4;
					intXPhepTinh2 = intXPhepTinh2 - 4;
					if ((intXPhepTinh > (-screenWidth - 10))
							&& (intXPhepTinh <= -screenWidth)) {
						istrue2 = false;
						intXPhepTinh = 0;
						intXPhepTinh2 = screenWidth;
					}
					myHandlerPhepTinh.sendMessage(msg);
					try {
						Thread.sleep(1);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		});
		return threadMovePhepTinh;
	}
}
